var express=require('express');
const{getMd5}=require('../utils/crypto');
const jwt=require('jsonwebtoken');
var router=express.Router();
const {repeat,add,login}=require('../service/merchantsService');
router.post('/repeat',async function(req,res,next){
    const username=req.body;
    const data=await repeat(username);
    res.send(data);
})
router.post('/add',async function(req,res,next){
    const {username,littlename, pass,checkPass,state}=req.body;
    const newpass=getMd5(pass);
    const data=await add({username,littlename,pass:newpass,state});
    res.send(data);
})
router.post('/login',async function(req,res,next){
    const {login_username,login_pass}=req.body;
    const newpass=getMd5(login_pass);
    const data=await login({login_username,login_pass:newpass});
    if(data.length>0){
        const token=jwt.sign({_id:data._id},'web0203',{expiresIn:1200});
        res.send({success:true,token});
    }else{
        res.send({success:false});
    }
})
router.get('/isLogin',async function(req,res,next){
    const token=req.get('Authorization');
})
module.exports=router;